﻿using System;
using UnityEngine;
using UnityEditor;
using HXu.BaseTools;

#if UNITY_EDITOR

namespace HXu.UnityEditor
{
    public class RenameObject : EditorWindow
    {
        private string _type = ""; // 类型前缀
        private string _name = ""; // 基础名称
        private int _id = 1;      // 起始ID
        private string _variation = ""; // 变体名称

        [MenuItem("EditorTools(HXu)/1.Rename Objects", false, 1)]
        static void CreateWindow()
        {
            //ScriptableWizard.DisplayWizard<Tools_ReplaceGameObject>("Replace GameObject");
            // 创建并显示窗口
            Rect theRect = new Rect(0, 0, 500, 500);
            RenameObject window = GetWindowWithRect<RenameObject>(theRect, false, "Rename Files");
            window.Show();
        }

        private void OnGUI()
        {
            // 显示帮助信息
            EditorGUILayout.HelpBox("Rename Objects.\n by HXu", MessageType.Info);
            EditorGUILayout.Space();

            // 输入字段
            _type = EditorGUILayout.TextField("Type:", _type);
            _name = EditorGUILayout.TextField("Name:", _name);
            _id = EditorGUILayout.IntField("Start ID:", _id);
            _variation = EditorGUILayout.TextField("Variation:", _variation);

            EditorGUILayout.Space();

            // 重命名按钮
            if (GUILayout.Button("Rename"))
            {
                try
                {
                    RenameSelectedObjects(); // 执行重命名
                    AssetDatabase.Refresh(); // 刷新资源数据库
                    _id = 1; // 重置ID
                }
                catch (Exception e)
                {
                    Debug.LogError($"Rename failed: {e.Message}");
                }
            }
        }

        /// <summary>
        /// 重命名选中的对象
        /// </summary>
        private void RenameSelectedObjects()
        {
            if (_name.IsNullOrWhiteSpace())
                throw new Exception("Name is null!");

            foreach (var asset in Selection.objects)
            {
                Debug.Log(asset.GetType());

                string newName = GenerateNewName(); // 生成新名称
                RenameAsset(asset, newName); // 重命名对象
                _id++; // 递增ID
            }
        }

        /// <summary>
        /// 生成新名称
        /// </summary>
        private string GenerateNewName()
        {
            string newName = _name + "_" + _id.ToString("d3"); // 基础名称 + ID

            if (!_type.IsNullOrWhiteSpace())
                newName = _type.Trim() + "_" + newName; // 添加类型前缀

            if (!_variation.IsNullOrWhiteSpace())
                newName = newName + "_" + _variation.Trim(); // 添加变体后缀

            return newName;
        }
        /// <summary>
        /// 重命名对象
        /// </summary>
        private void RenameAsset(UnityEngine.Object asset, string newName)
        {
            if (asset is GameObject && !AssetDatabase.Contains(asset))
            {
                // 重命名场景中的GameObject
                Undo.RecordObject(asset, "Rename GameObject");
                ((GameObject)asset).name = newName;
            }
            else if (AssetDatabase.Contains(asset))
            {
                // 重命名资源文件
                string path = AssetDatabase.GetAssetPath(asset);
                AssetDatabase.RenameAsset(path, newName);
            }
        }
    }
}
#endif
